package com.lm.activiti.boot.cases;

import com.google.common.collect.*;
import com.lm.activiti.boot.cases.dto.ParentProperties;
import org.junit.Test;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author lm_jcf
 * @date 2020/3/24 14:26
 */
public class StreamDemo {

    public static List<ParentProperties> getDataStack() {
        ParentProperties parentProperties = new ParentProperties();
        parentProperties.setName("张三");
        parentProperties.setAge(20);

        ParentProperties parentProperties1 = new ParentProperties();
        parentProperties1.setName("李四");
        parentProperties1.setAge(20);

        ParentProperties parentProperties2 = new ParentProperties();
        parentProperties2.setName("王五");
        parentProperties2.setAge(21);

        List<ParentProperties> list = Lists.newArrayList(parentProperties, parentProperties1, parentProperties2);

        return list;
    }

    @Test
    public void updateStreamElement() {

        List<ParentProperties> list = getDataStack();
        Map<Integer, ParentProperties> collect = list.stream().collect(Collectors.toMap(ParentProperties::getAge, Function.identity(), (key1, key2) -> key2));

        List<ParentProperties> filterList = list.stream().filter(element -> element.getAge() == 21).collect(Collectors.toList());

        Iterator<ParentProperties> iterator = filterList.iterator();
        while (iterator.hasNext()) {
            ParentProperties next = iterator.next();
            if (next.getAge() == 21) {
                list.remove(next);
            }
        }

        System.out.println(list);

    }

    @Test
    public void multimaps_Index() {

        List<ParentProperties> list = getDataStack();
        Map<Integer, ParentProperties> collect = list.stream().collect(Collectors.toMap(ParentProperties::getAge, Function.identity(), (key1, key2) -> key2));

        ImmutableListMultimap<Integer, ParentProperties> index = Multimaps.index(list, p -> p.getAge());
        for (Integer age : index.keySet()) {

            ImmutableList<ParentProperties> parentProperties3 = index.get(age);
            System.out.println(age + ":");
            for (ParentProperties properties : parentProperties3) {
                properties.setId(1);
                System.out.println(properties.getName());
            }
        }

        Multimaps.index(list, p -> p.getAge());

    }

    @Test
    public void sorted() {
        List<ParentProperties> list = getDataStack();

//        Collections.sort(list, (e1, e2) -> e2.getAge() - e1.getAge());

//        list.stream().sorted(Comparator.comparing(ParentProperties::getAge));

        System.out.println(list);

    }

    @Test
    public void collection_find_value() {
        List<ParentProperties> list = getDataStack();

        boolean present = list.stream().filter(element -> element.getName().equals("张三1")).findAny().isPresent();

        System.out.println(present);
    }

    @Test
    public void iteration_iteration() {
        List<ParentProperties> list = getDataStack();

        List<Integer> ageList = Lists.newArrayList(18, 19, 20);

        Set<Integer> collect = list.stream().map(ParentProperties::getAge).collect(Collectors.toSet());

//        ageList.stream().filter(age -> collect.contains(age)).collect(Collectors.toList());

        ageList.stream().filter(age -> list.stream().map(ParentProperties::getAge).collect(Collectors.toSet()).contains(age)).collect(Collectors.toList());

        System.out.println(ageList);
    }


    @Test
    public void compareStreamAndFor() {
        List<ParentProperties> list = Lists.newArrayList();

        for (int i = 0; i < 100000; i++) {
            list.addAll(getDataStack());
        }

        List<ParentProperties> list_3 = Lists.newArrayList();
        System.out.println("stream--开始时间戳：" + System.currentTimeMillis());
        Stream<ParentProperties> stream = list.stream();
        System.out.println("stream--结束时间戳：" + System.currentTimeMillis());
        System.out.println("filter--开始时间戳：" + System.currentTimeMillis());
        list_3.addAll(stream.filter(element -> element.getAge() == 20 && element.getName().equals("张三")).collect(Collectors.toList()));
        System.out.println("filter--结束时间戳：" + System.currentTimeMillis());

        System.out.println("filter2--开始时间戳：" + System.currentTimeMillis());
        list_3.addAll(stream.filter(element -> element.getAge() == 20 && element.getName().equals("张三")).collect(Collectors.toList()));
        System.out.println("filter2--结束时间戳：" + System.currentTimeMillis());



        System.out.println("开始时间戳：" + System.currentTimeMillis());
        List<ParentProperties> list_2 = Lists.newArrayList();
        for (ParentProperties parentProperties : list) {
            if (parentProperties.getAge() == 21 && parentProperties.getName().equals("张三")) {
                list_2.add(parentProperties);
            }
        }
        System.out.println("结束时间戳：" + System.currentTimeMillis());

    }

    @Test
    public void filter(){
        List<ParentProperties> list = Lists.newArrayList();

        list.stream().filter(element -> element.getName().equals("")).collect(Collectors.toList());
    }

    @Test
    public void streamNull(){

        List<ParentProperties> list = getDataStack();

        System.out.println(list.stream().filter(element -> element.getAge() == 25).findFirst().get().getName());


    }

}
